import {
  ADD_NUMBER,
  SUB_NUMBER,
  
  SET_BANNER,
  SET_RECOMMEND
} from './constants.js'

// const initialState = {
//   counter: 0,
//   banners: [],
//   recommends: []
// }
// function reducer(state = initialState, action) {
//   switch(action.type) {
//     case ADD_NUMBER:
//       return {...state, counter: state.counter + action.num}
//     case SUB_NUMBER:
//       return {...state, counter: state.counter - action.num}
//     case SET_BANNER:
//       return {...state, banners: action.list}
//     case SET_RECOMMEND:
//       return {...state, recommends: action.list}
//     default:
//       return state
//   }
// }

//拆分counterReducer
const countInitialState = {
  counter: 0
}
function counterReducer(state = countInitialState, action) {
  switch(action.type) {
    case ADD_NUMBER:
      return {...state, counter: state.counter + action.num};
    case SUB_NUMBER:
      return {...state, counter: state.counter - action.num};
    default:
      return state
  }
}

//拆分homeReducer
const homeInitialState = {
  banners: [],
  recommends: []
}
function homeReducer(state = homeInitialState, action) {
  switch(action.type) {
    case SET_BANNER:
      return {...state, banners: action.list}
    case SET_RECOMMEND:
      return {...state, recommends: action.list}
    default:
      return state
  }
}

function reducer(state = {}, action) {
  return {
                // {}里取counterInfo为undefined 就会使用上面定义的默认值
    counterInfo: counterReducer(state.counterInfo, action),
    homeInfo: homeReducer(state.homeInfo, action)
  } 
}

export default reducer